package com.lhm.controller;


import com.lhm.entity.*;
import com.lhm.service.CartService;

;
import com.lhm.service.OrderService;
import com.lhm.tool.RespJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.context.SecurityContextHolder;

import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/api/fore/cart")
public class CartController {
    @Autowired
    CartService cartService;

    @Autowired
    OrderService orderService;

    //根据 getUser 拿到Token相应的登录用户的账号
    public static Object getUser(){
        Object object = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return object;
    }
    @RequestMapping("/findAll")
    // List<CartBook> findByUid(int uid)
    public Map<String, Object> findAllCartByUid() {
        Map<String, Object> map = new HashMap();
        MyUser mu= (MyUser) getUser();//根据myuser 拿到Token相对应的id
     //   System.out.println("========================");
        System.out.println(mu);//打印一下mu
        //如果id小于0，就没有登录，反则就登录成功，创建一个数组[CartBook]
        int uid=mu.getId();
        if (uid< 0) {
            return RespJson.pack(401, "未登录",null);
        } else {

            List<CartBook> lst =cartService.findByUid(mu.getId());//创建一个数组
            System.out.println("++++++++++++");
            System.out.println(lst);
            map.put("cart", lst);
            return RespJson.pack(200, "", lst);
        }
    }

    @RequestMapping("/addCart2Order")
    @ResponseBody

    public Map<String, Object> addCart2Order(@RequestBody List<Orditem>items){
             new HashMap();
        MyUser mu= (MyUser) getUser();//根据myuser 拿到Token相对应的id
        System.out.println(mu);//打印一下mu
        //如果id小于0，就没有登录,如果登录成功,成功接收到数据, 但更新订单失败,就失败，
        //如果登录成功的话会成功接收到数据，并产生订单
        int uid=mu.getId();
        if (uid<0){
            return RespJson.pack(401, "未登录或登录超时", null);
        } else {
            System.out.println(items);
         //   System.out.println("//////////////////////");
            Order order = new Order();
            System.out.println(order);
         //   System.out.println("**********************");
            order.setUid(uid);
            System.out.println(uid);
         //   System.out.println("++++++++++++++++");
            System.out.println("/addCart2Order 参数: order=" + order + "\r\n items=" + items);
            try {
                orderService.addOrdItem(order,items);
            } catch (Exception E){
                return RespJson.pack(500, "成功接收到数据, 但更新订单失败 ....", null);
            }
            System.out.println(order);
            return RespJson.pack(200, "成功接收到数据, 并产生订单 ....", order);
        }

    }


    /**
     * 自动转换日期类型的字段格式
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

}
